package admin

import (
	"context"

	"suci-go-zero/common/ctxdata"
	"suci-go-zero/common/xerr"
	"suci-go-zero/internal/model"
	"suci-go-zero/internal/svc"
	"suci-go-zero/internal/types"

	"github.com/pkg/errors"
	"github.com/zeromicro/go-zero/core/logx"
)

type UserDelLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUserDelLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserDelLogic {
	return &UserDelLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *UserDelLogic) UserDel(req *types.Ids) error {
	userId := ctxdata.GetUidFromCtx(l.ctx)
	if userId == int64(req.Ids[0]) {
		return errors.Wrap(xerr.NewErrCode(xerr.USER_NOT_DEL_SELF), "不能删除自己")
	}
	for _, id := range req.Ids {
		user, err := l.svcCtx.Models.User.FindOne(l.ctx, int64(id))
		if err != nil && err != model.ErrNotFound {
			return errors.Wrap(xerr.NewErrCode(xerr.DB_ERROR), "删除用户,用户查找,数据可出错")
		}
		err = l.svcCtx.Models.User.DeleteSoft(l.ctx, nil, user)
		if err != nil {
			return errors.Wrap(xerr.NewErrCode(xerr.DB_ERROR), "删除用户,用户查找,数据可出错")
		}
	}

	return nil
}
